//
//  PopoverAnimator.swift
//  JGWB
//
//  Created by 付建国 on 2016/10/10.
//  Copyright © 2016年 sher. All rights reserved.
//

import UIKit

class PopoverAnimator: NSObject {
    
    var isPresented : Bool = false
    
    // MARK:- 对外提供属性
    var presentedFrame : CGRect = CGRect.zero
    
    var callBack : ((_ isPresented : Bool) -> ())?
    
    // MARK:- 自定义构造函数
    init(callBack : @escaping (_ isPresented : Bool) -> ()) {
        self.callBack = callBack
    }
    
}


// MARK:- 自定义转场方法
extension PopoverAnimator : UIViewControllerTransitioningDelegate{
    //改变view弹出的尺寸
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        
        let presentation = JGPresenttationController.init(presentedViewController: presented, presenting: presenting)
        presentation.presentedFrame = presentedFrame
        
        return presentation
    }
    
    //自定义弹出动画
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = true
        
        callBack!(isPresented)
        
        return self
    }
    
    //自定义消失动画
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        
        callBack!(isPresented)
        
        return self
    }
    
}

// MARK:- 弹出动画和消失动画的方法
extension PopoverAnimator : UIViewControllerAnimatedTransitioning{
    //动画执行的时间
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        
        return 0.5
    }
    //获取转场上下文：可以通过转场上下文获取弹出的view和消失的view
    //UITransitionContextFromViewKey : 获取消失的view
    //UITransitionContextToViewKey : 获取弹出的view
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        
        isPresented ? animationForPresentedView(using: transitionContext) : animationForDismissedView(using: transitionContext)
    }
    
    /// 自定义弹出动画
    fileprivate func animationForPresentedView(using transitionContext: UIViewControllerContextTransitioning){
        //获取弹出的view
        let presentedView = transitionContext.view(forKey: UITransitionContextViewKey.to)!
        
        //将弹出view添加到containerView中
        transitionContext.containerView.addSubview(presentedView)
        
        //添加动画
        presentedView.transform = CGAffineTransform.init(scaleX: 1.0, y: 0.0)
        presentedView.layer.anchorPoint = CGPoint.init(x: 0.5, y: 0)
        
        UIView.animate(withDuration: self.transitionDuration(using: transitionContext), animations: {
            
            presentedView.transform = CGAffineTransform.identity
            
        }) { (_) in
            //必须告诉上下文已经完成动画
            transitionContext.completeTransition(true)
            
        }
    }
    
    /// 自定义消失动画
    fileprivate func animationForDismissedView(using transitionContext: UIViewControllerContextTransitioning){
        
        //获取消失的view
        let dismissedView = transitionContext.view(forKey: UITransitionContextViewKey.from)!
        
        //将弹出view添加到containerView中
        transitionContext.containerView.addSubview(dismissedView)
        
        UIView.animate(withDuration: self.transitionDuration(using: transitionContext), animations: {
            
            dismissedView.transform = CGAffineTransform.init(scaleX: 1.0, y: 0.00001)
            
        }) { (_) in
            //必须告诉上下文已经完成动画
            dismissedView.removeFromSuperview()
            transitionContext.completeTransition(true)
            
        }
        
    }
}
